﻿using CommonTools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using shop.Model;

namespace shop.Bll.BasicInfo
{

    /// <summary> 现金分类/Cash Category </summary>
    public class JCashCategoryBll : BasicBllParent, IFastQueryBll<JCashCategory>
    {


        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="code">查询代码</param>
        /// <param name="isDetails">是否明细</param>
        /// <param name="msg">错误消息</param>
        /// <returns></returns>
        public override IEnumerable<object> Query(string code, bool isDetails, out string msg)
        {
            msg = null;
            if (isDetails)
            {

                var list = DB.FSql.Select<JCashCategory>().Where(s => s.Isdel == false);
                if (string.IsNullOrEmpty(code) == false)
                {
                    list = list.Where(a => a.Code.StartsWith(code));
                }

                return list.ToList().OrderBy(a => a.Code)
                    .ToList<object>();      //排序

            }
            else
            {
                return new List<object>();
            }

        }




        #region 分组: 增删改

        /// <summary> 添加分类 </summary>
        public override bool AddGroup(object obj, IUser user, out string msg)
        {
            msg = null;
            if (!(obj is JCashCategory model))
            {
                msg = Model.CN.Msg_SystemError; // "辅助资料为空";
                return false;
            }

            var id = DB.FSql.Insert(model).ExecuteIdentity();
            model.ID = (int)id;


            bool bl = model.ID != 0;

            if (bl)     //日志
            {
                DB.FSql.Insert(new CLogs()
                {
                    Computer = user.ComputerInfo,
                    DocId = model.ID.ToString(),
                    DocAction = "添加分类",
                    DocName = nameof(JCashCategory),
                    Note = $"代码:{model.Code},名称:{model.Name}",
                }).ExecuteAffrows();

            }

            return true;

        }

        /// <summary> 删除分类 </summary>
        public override bool DeleteGroup(object obj, IUser user, out string msg)
        {
            msg = null;
            if (!(obj is JCashCategory model))
            {
                msg = Model.CN.Msg_InformationCannotBeBlank; // "辅助资料为空";
                return false;
            }

            if (this.ClassChk(model) == false)     //检查分类下是否由明细
            {

                var item = DB.FSql.Select<JCashCategory>().FirstOrDefault(a => a.Isdel == false && a.Code == model.Code);

                model.Isdel = true;

                DB.FSql.Update<JCashCategory>()
                    .Where(a => a.ID == model.ID)
                    .Set(a => a.Isdel, true)
                    .ExecuteAffrows();


                //日志
                DB.FSql.Insert(new CLogs()
                {
                    Computer = user.ComputerInfo,
                    DocId = item.ID.ToString(),
                    DocAction = "删除分类",
                    DocName = nameof(JCashCategory),
                    Note = $"代码:{item.Code},名称:{item.Name}",
                }).ExecuteAffrows();

                return true;

            }
            else
            {
                msg = CN.Msg_GroupHasItemsCannotDelete;  // "此分组下还有项目, 不能删除!";
            }
            return false;
        }



        /// <summary> 分类下的是否有明细 </summary>
        public bool ClassChk(JCashCategory model)
        {
            return DB.FSql.Select<JCashCategory>()
                .Any(a => a.ID != model.ID
                                       && a.Code.StartsWith(model.Code)
                                       && a.Isdel == false);
        }



        /// <summary> 修改分组 </summary>
        public override bool UpdateGroup(object obj, IUser user, out string err)
        {
            err = "";
            if (!(obj is JCashCategory model))
            {
                err = Model.CN.Msg_InformationCannotBeBlank; // "辅助资料为空";
                return false;
            }


            model.Isdel = false;
            model.Name = model.Name;
            DB.FSql.Update<JCashCategory>()
                .Where(a => a.Isdel == false && a.Code == model.Code)
                .Set(a => a.Name, model.Name)
                .ExecuteAffrows();

            //日志
            DB.FSql.Insert(new CLogs()
            {
                Computer = user.ComputerInfo,
                DocId = model.ID.ToString(),
                DocAction = "修改分类",
                DocName = nameof(JCashCategory),
                Note = $"代码:{model.Code},名称:{model.Name}",
            }).ExecuteAffrows();


            return true;


        }

        #endregion


        #region 明细: 增删改


        /// <summary> 填加分类 </summary>
        public override bool Insert(object obj, IUser user, out string msg)
        {
            msg = null;
            if (!(obj is JCashCategory model))
            {
                msg = Model.CN.Msg_InformationCannotBeBlank; // "辅助资料为空";
                return false;
            }
            model.Isdel = false;

            if (string.IsNullOrEmpty(model.Code))
            {
                msg = CN.Msg_CodeCannotBeBlank;   // "资料代码不能为空";
                return false;
            }
            if (string.IsNullOrEmpty(model.Name))
            {
                msg = CN.Msg_NameCannotBeBlank;   // "资料名称不能为空";
                return false;
            }
            if (this.Query(model.Code, true, out var _).Count() > 0)
            {
                msg = CN.Msg_CodeCannotRepeat;    // "代码不能重复";
                return false;
            }

            var id = DB.FSql.Insert(model).ExecuteIdentity();
            model.ID = (int)id;


            bool bl = model.ID != 0;

            if (bl)     //日志
            {
                DB.FSql.Insert(new CLogs()
                {
                    Computer = user.ComputerInfo,
                    DocId = model.ID.ToString(),
                    DocAction = "添加",
                    DocName = nameof(JCashCategory),
                    Note = $"代码:{model.Code},名称:{model.Name}",
                }).ExecuteAffrows();

            }

            return true;
        }


        /// <summary> 修改 </summary>
        public override bool Update(object obj, IUser user, out string msg)
        {
            msg = null;
            if (!(obj is JCashCategory model))
            {
                msg = Model.CN.Msg_InformationCannotBeBlank; // "辅助资料为空";
                return false;
            }
            if (model.ID <= 0)
            {
                msg = CN.Msg_IdCannotRepeat;   // "资料ID不能为空";
                return false;
            }
            if (string.IsNullOrEmpty(model.Code))
            {
                msg = CN.Msg_CodeCannotBeBlank;   // "资料代码不能为空";
                return false;
            }
            if (string.IsNullOrEmpty(model.Name))
            {
                msg = CN.Msg_NameCannotBeBlank;   // "资料名称不能为空";
                return false;
            }

            DB.FSql.Update<JCashCategory>()
                .SetSource(model)
                .ExecuteAffrows();

            //日志
            DB.FSql.Insert(new CLogs()
            {
                Computer = user.ComputerInfo,
                DocId = model.ID.ToString(),
                DocAction = "修改",
                DocName = nameof(JCashCategory),
                Note = $"代码:{model.Code},名称:{model.Name}",
            }).ExecuteAffrows();

            return true;

        }


        /// <summary> 删除 </summary>
        public override bool Delete(object obj, IUser user, out string msg)
        {
            msg = null;
            if (!(obj is JCashCategory model))
            {
                msg = CN.Msg_SystemError;
                return false;
            }
            if (model.ID <= 0)
            {
                msg = CN.Msg_IdCannotRepeat;   // "资料ID不能为空";
                return false;
            }

            model.Isdel = true;
            DB.FSql.Update<JCashCategory>()
                .Where(a => a.ID == model.ID)
                .Set(a => a.Isdel, true)
                .ExecuteAffrows();


            //日志
            DB.FSql.Insert(new CLogs()
            {
                Computer = user.ComputerInfo,
                DocId = model.ID.ToString(),
                DocAction = "删除",
                DocName = nameof(JCashCategory),
                Note = $"代码:{model.Code},名称:{model.Name}",
            }).ExecuteAffrows();

            return true;
        }

        public override bool Check(object obj, IUser user, out string err)
        {
            err = "";
            if (obj is JCashCategory model)
            {
                //DB.FSql.Update<JCashCategory>()
                //  .Where(a => a.ID == model.ID)
                //  .Set(a => a., true)
                //  .ExecuteAffrows();

                //DB.FSql.Insert(new c_logs() { DocId = model.ID.ToString(), DocAction = "审核", DocName = nameof(JCashCategory), zdr = user.Name, Computer = user.ComputerInfo })
                //    .ExecuteAffrows();


                return true;
            }
            else
            {
                err = CN.Msg_SystemError;
                return false;
            }
        }

        public override bool UnCheck(object obj, IUser user, out string err)
        {
            if (obj is JCashCategory model)
            {
                //DB.FSql.Update<JCashCategory>()
                //  .Where(a => a.ID == model.ID)
                //  .Set(a => a., false)
                //  .ExecuteAffrows();

                //DB.FSql.Insert(new c_logs() { DocId = model.ID.ToString(), DocAction = "审核", DocName = nameof(JCashCategory), zdr = user.Name, Computer = user.ComputerInfo })
                //    .ExecuteAffrows();


                err = "";
                return true;
                //return dal.UnCheck(model, user, out err);
            }
            else
            {
                err = CN.Msg_SystemError;
                return false;
            }
        }



        #endregion



        #region 实现IFastQueryBll 接口, 在FastBox中使用

        /// <summary> 在查询窗体,报表中显式的名称 </summary>
        public string Title => "现金分类/Cash Category";

        /// <summary> 本地存储的明细 </summary>
        /// <remarks>如果基础资每次都从数据库查询, 这个没用, 保持null</remarks>
        public List<JCashCategory> Items => null;


        /// <summary> 是否每次从数据库查询 </summary>
        public bool QueryFromDataBase => true;


        /// <summary> 存储在快速查询窗体中,显示的属性,及属性的显示, 这个暂时不用,以后删除 </summary>
        public Dictionary<string, string> DisplayColumns => null;


        /// <summary> 显示的列, 静态 </summary>
        private static readonly string[] _columns = new string[] { nameof(JCashCategory.Code), nameof(JCashCategory.Name) };


        /// <summary> 存储在快速查询窗体中,显示的属性 </summary>
        public string[] Columns => _columns;


        /// <summary> 实体模型的代码属性名称 </summary>
        public string CodeField => nameof(JCashCategory.Code);



        /// <summary> 代码中级别的分隔符 </summary>
        public override string Separator => ".";


        /// <summary> 查询树形图的所有节点 </summary>
        public override IEnumerable<IFastTree> QueryTreeList()
        {
            return new List<IFastTree>() { };

            //var rst = _db.JCashCategory.AsNoTracking().Where(a => a.Isdel == false);

            //return rst.ToList<IFastTree>().OrderBy(a=> a.Code).ToList();
        }


        /// <summary> 查询方法 </summary>
        public List<JCashCategory> Query(string findText, string prefixCode = null)
        {
            var list = DB.FSql.Select<JCashCategory>()
                .Where(a => a.Isdel == false
                  && (a.Name.Contains(findText) || a.Code.Contains(findText)));

            if (string.IsNullOrEmpty(prefixCode) == false)
            {
                list = list.Where(a => a.Code.StartsWith(prefixCode));
            }

            return list.ToList().OrderBy(a => a.Code).ToList();
        }


        /// <summary> 查询树形图的一个节点下的所有内容 </summary> 
        public List<JCashCategory> QueryByGroup(string group)
        {
            return DB.FSql.Select<JCashCategory>()
                .Where(w => w.Isdel == false && w.Code.StartsWith(group))
                .ToList()
                .OrderBy(a => a.Code)
                .ToList();        //排序
        }


        #endregion


        #region 权限

        /// <summary> 查询导出权限 </summary>
        public override string LookRight => "01.26.1";

        /// <summary> 保存权限 </summary>
        public override string SaveRight => "01.26.2";

        /// <summary> 修改别人单据权限 </summary>
        public string ModifyRight => "01.26.3";

        /// <summary> 删除别人单据权限 </summary>
        public string DeleteRight => "01.26.4";

        /// <summary> 审核,反审核权限 </summary>
        public override string CheckRight => "01.26.5";

        #endregion



        #region 其他


        /// <summary> 使用代码查询一个 </summary>
        public JCashCategory GetByCode(string code)
        {
            return DB.FSql.Select<JCashCategory>().FirstOrDefault(a => a.Isdel == false && a.Code == code);
        }


        #endregion



        #region 按钮,事件





        #endregion

    }



}
